package com.oa.framework.org.department.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.exception.CreateObjectException;
import com.oa.common.exception.UpdateObjectException;
import com.oa.common.tree.TreeUtils;
import com.oa.framework.FrameworkEngine;
import com.oa.framework.org.department.dao.DepartmentDao;
import com.oa.framework.org.department.domain.Department;
import com.oa.framework.org.domain.Duty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cuichao
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;


    @Override
    public List<Department> loadDepartments() {
        QueryWrapper<Department> query = new QueryWrapper<>();
        query.eq("disable",0);
        return departmentDao.selectList(query);
    }


    @Override
    public Department loadFoldDepartment() {
        List<Department> departments = departmentDao.selectList(null);
        List<Department> fold = TreeUtils.buildTree(departments, null);
        return fold.get(0);
    }


    @Override
    public List<Department> loadDepartmentIncludeDuties() {
        return departmentDao.loadDepartmentIncludeDuties();
    }

    @Override
    public Department getDepartment(Long deptId) {
        return departmentDao.selectById(deptId);
    }

    @Override
    public Department getDepartmentIncludeDuties(Long deptId) {
        return departmentDao.getDepartmentIncludeDuties(deptId);
    }


    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean addDepartment(Department department) throws UpdateObjectException {
        final int createSuccessFlag = 1;
        //查找masterDutyId映射的positionI 并注入
        if(department.getMasterDutyId() == null ){
            throw new UpdateObjectException();
        }
        Duty dutyDetail = FrameworkEngine.getDutyService().getDutyDetail(department.getMasterDutyId());
        Long masterPositionId = dutyDetail.getPostId();
        department.setMasterPositionId(masterPositionId);
        int flag = departmentDao.insert(department);
        if (createSuccessFlag != flag) {
            throw new UpdateObjectException();
        }
        //添加department与Duty的关系
        departmentDao.buildDepartmentDutyLink(department.getDepartmentId(),department.getDuties().stream().map(v -> v.getDutyId()).collect(Collectors.toList()));
        return true;
    }


    @Transactional(rollbackFor = {Exception.class})
    @Override
    public boolean updateDepartment(Department department) throws CreateObjectException {
        final int updateSuccessFlag = 1;
        //查找masterDutyId映射的positionI 并注入
        if(department.getMasterDutyId() == null ){
            throw new UpdateObjectException();
        }
        int flag = departmentDao.updateById(department);
        if (updateSuccessFlag != flag) {
            throw new CreateObjectException();
        }
        //先移除department和Duty关系在建立新的
        departmentDao.removeDepartmentDutyLink(department.getDepartmentId());

        departmentDao.buildDepartmentDutyLink(department.getDepartmentId(),department.getDuties().stream().map(v -> v.getDutyId()).collect(Collectors.toList()));
        return true;
    }


    @Override
    public boolean disable(Long departmentId) {
        final int disableSuccessFlag = 1;
        int flag = departmentDao.disableDepartment(departmentId);
        if (disableSuccessFlag != flag) {
            return false;
        }
        return true;
    }


}
